﻿Imports System.Data.OleDb
Imports DAO
Imports DTO
Imports BUS
Public Class FormPhieuNhapSach

#Region "Bien toan cuc"
    Private BangSach As New DataTable
    Private dssach As New List(Of Sach_DTO)()
    Private masachdangchon As Integer = -1
    Private flag As Integer = 0
#End Region

#Region "Đổi màu nút"
    Private Sub MauMoi(ByRef btn As Button, ByVal trangThai As Integer)

        If trangThai = 1 Then
            btn.BackColor = Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(128, Byte), Integer), CType(CType(255, Byte), Integer))
        Else btn.BackColor = Color.Gray
        End If


    End Sub
#End Region

#Region "Load Form"

    Private Sub FormPhieuNhapSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ButtonSua.Enabled = False
        ButtonXoa.Enabled = False

        LayTheLoai(ComboBoxTheLoai)
        LayTen(ComboBoxTen)
        ComboBoxTen.SelectedIndex = -1
        ComboBoxTheLoai.SelectedIndex = -1
        flag = 1

    End Sub

#End Region

#Region "Load Bang The Loai len Combobox"

    Public Shared Sub LayTheLoai(ByVal cbo As ComboBox)
        Dim BangTheLoai As DataTable = TheLoai_BUS.LoadTheLoai()
        cbo.DataSource = BangTheLoai
        cbo.DisplayMember = "TenTheLoai"
        cbo.ValueMember = "MaTheLoai"
    End Sub

#End Region

#Region "Load Ten Sach len Combobox"

    Public Sub LayTen(ByVal cbo As ComboBox)
        BangSach = Sach_BUS.LaySach()
        cbo.DataSource = BangSach
        cbo.DisplayMember = "TenSach"
        cbo.ValueMember = "MaSach"
        Clear()
    End Sub

#End Region

#Region "Load DataGridView"

    Private Sub LoadGridView()
        Dim stt As Integer = 1
        DataGridView1.Rows.Clear()
        For Each s As Sach_DTO In dssach
            Dim sql As String = String.Format("select TenTheLoai from TheLoai where MaTheLoai = {0}", s.MaTL)
            Dim tentl As String = DataProvider.ExcuteScalar(sql).ToString()
            DataGridView1.Rows.Add(stt.ToString(), s.Ten, tentl, s.TacGia, s.SoLuong, s.DonGia)
            stt = stt + 1
        Next
        DataGridView1.ClearSelection()
    End Sub

#End Region

#Region "Clear cac control nhap"

    Private Sub Clear()
        ComboBoxTen.Text = ""
        ComboBoxTheLoai.SelectedIndex = -1
        TextBoxTacGia.Text = ""
        TextBoxSoLuong.Text = ""
        TextBoxGia.Text = ""
    End Sub

#End Region

#Region "Kiểm tra Dữ Liệu"

    Public Function KiemTra() As Integer
        Dim flag As Integer = 1
        If flag = 1 Then
            If (ComboBoxTen.Text = "") Then
                MessageBox.Show("Tên sách không được để trống", "Thông báo")
                flag = 0
                ComboBoxTen.Focus()
            End If
        End If

        If flag = 1 Then
            If (TextBoxTacGia.Text = "") Then
                MessageBox.Show("Tác giả không được để trống", "Thông báo")
                flag = 0
                TextBoxTacGia.Focus()
            End If
        End If

        If flag = 1 Then
            Try
                Dim gia As Double = Double.Parse(TextBoxGia.Text)
            Catch ex As Exception
                MessageBox.Show("Đơn giá không hợp lệ", "Thông báo")
                flag = 0
                TextBoxGia.Focus()
            End Try
        End If

        If flag = 1 Then
            Try
                Dim sl As Double = Double.Parse(TextBoxSoLuong.Text)
            Catch ex As Exception
                MessageBox.Show("Số lượng không hợp lệ", "Thông báo")
                flag = 0
                TextBoxSoLuong.Focus()
            End Try
        End If

        Return flag
    End Function
#End Region

#Region "Xu ly nut Xac Nhan"

    Private Sub ButtonXacNhan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonXacNhan.Click
        If masachdangchon = -1 Then
            If KiemTra() = 1 Then
                Dim sach As New Sach_DTO(ComboBoxTen.Text, ComboBoxTheLoai.SelectedValue, TextBoxTacGia.Text, TextBoxSoLuong.Text, TextBoxGia.Text)
                If PhieuNhap_BUS.KiemTraQuyDinhNhapSach(sach) = 0 Then
                    MessageBox.Show("Vi phạm Quy Định nhập sách", "Thông báo")
                Else
                    dssach.Add(sach)
                    Clear()
                    LoadGridView()
                    TextBoxTacGia.ReadOnly = False
                    ComboBoxTheLoai.Enabled = True
                End If
            End If
        Else
            MessageBox.Show("Đang sửa Sách", "Thông báo")
        End If
    End Sub

#End Region

#Region "Xu ly nut Xoa"

    Private Sub ButtonXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonXoa.Click
        If masachdangchon > -1 Then
            dssach.RemoveAt(masachdangchon)
            Clear()
            LoadGridView()
            masachdangchon = -1
            ButtonSua.Enabled = False
            ButtonXoa.Enabled = False
            ButtonXacNhan.Enabled = True
        Else
            MessageBox.Show("Chưa chọn sách để xóa", "Thông báo")
        End If
    End Sub

#End Region

#Region "Xu ly nhap chuot single vao 1 dong trong DataGridView"

    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        ButtonXacNhan.Enabled = False
        ButtonSua.Enabled = True
        ButtonXoa.Enabled = True
        Dim index As Integer = DataGridView1.CurrentRow.Index
        Dim stt As Integer = Integer.Parse(DataGridView1.Rows(index).Cells(0).Value)
        masachdangchon = stt - 1
        ComboBoxTen.Text = dssach(masachdangchon).Ten
        ComboBoxTheLoai.Text = DataGridView1.Rows(index).Cells(2).Value
        TextBoxTacGia.Text = dssach(masachdangchon).TacGia
        TextBoxSoLuong.Text = dssach(masachdangchon).SoLuong
        TextBoxGia.Text = dssach(masachdangchon).DonGia
    End Sub

#End Region

#Region "Xu ly nut Sua"

    Private Sub ButtonSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSua.Click
        If masachdangchon > -1 Then
            dssach(masachdangchon).Ten = ComboBoxTen.Text
            dssach(masachdangchon).MaTL = ComboBoxTheLoai.SelectedValue
            dssach(masachdangchon).TacGia = TextBoxTacGia.Text
            dssach(masachdangchon).SoLuong = TextBoxSoLuong.Text
            dssach(masachdangchon).DonGia = TextBoxGia.Text
            Clear()
            LoadGridView()
            masachdangchon = -1
            ButtonSua.Enabled = False
            ButtonXoa.Enabled = False
            ButtonXacNhan.Enabled = True
        Else
            MessageBox.Show("Chưa chọn sách để sửa", "Thông báo")
        End If
    End Sub

#End Region

#Region "Xu ly nut Lap Phieu && Nhap Sach"

    Private Sub ButtonNhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonThem.Click
        If dssach.Count > 0 Then
            Try
                Dim pn As New PhieuNhap_DTO(DTPNgayNhap.Value)
                PhieuNhap_BUS.NhapPhieu(pn)

                Dim sql As String = "select max(MaPhieuNhap) from PhieuNhap"
                Dim mapn As Integer = DataProvider.ExcuteScalar(sql)

                For Each s As Sach_DTO In dssach
                    Sach_BUS.NhapSach(s)
                    sql = "select max(MaSach) from Sach"
                    Dim masach As Integer = DataProvider.ExcuteScalar(sql)

                    PhieuNhap_BUS.NhapChiTiet(mapn, masach, s.SoLuong)
                Next

                dssach.Clear()
                DataGridView1.Rows.Clear()
                Clear()
                masachdangchon = -1
                MessageBox.Show("Nhập Sách thành công", "Thông báo")
            Catch ex As Exception
                MessageBox.Show("Nhập Sách thất bại", "Thông báo")
            End Try
        Else
            MessageBox.Show("Yêu cầu bấm Xác Nhận sách trước khi Thêm", "Thông báo")
        End If
    End Sub

#End Region

#Region "Xu ly nut Thoat"

    Private Sub ButtonThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonThoat.Click
        Me.Close()
    End Sub

#End Region

#Region "Xử lý nút Xóa"

    Private Sub ButtonClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonClear.Click
        Clear()
        ButtonSua.Enabled = False
        ButtonXoa.Enabled = False
        masachdangchon = -1
        DataGridView1.ClearSelection()
        ButtonXacNhan.Enabled = True
        TextBoxTacGia.ReadOnly = False
        ComboBoxTheLoai.Enabled = True
    End Sub
#End Region


#Region "Xử lý selectedchanged của combobox tên"
    Private Sub ComboBoxTen_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxTen.SelectedIndexChanged
        If flag = 1 Then
            Dim index As Integer = ComboBoxTen.SelectedIndex
            TextBoxTacGia.Text = BangSach.Rows(index).Item(3).ToString()
            ComboBoxTheLoai.SelectedValue = BangSach.Rows(index).Item(2).ToString()
            TextBoxGia.Text = BangSach.Rows(index).Item(5).ToString()
            TextBoxTacGia.ReadOnly = True
            ComboBoxTheLoai.Enabled = False
        End If
    End Sub
#End Region


    Private Sub ButtonXoa_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonXoa.EnabledChanged

        If ButtonXoa.Enabled Then
            MauMoi(ButtonXoa, 1)
        Else
            MauMoi(ButtonXoa, 2)
        End If

    End Sub

    Private Sub ButtonXacNhan_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonXacNhan.EnabledChanged
        If ButtonXacNhan.Enabled Then
            MauMoi(ButtonXacNhan, 1)
        Else
            MauMoi(ButtonXacNhan, 2)
        End If
    End Sub

    Private Sub ButtonSua_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSua.EnabledChanged
        If ButtonSua.Enabled Then
            MauMoi(ButtonSua, 1)
        Else
            MauMoi(ButtonSua, 2)
        End If
    End Sub
End Class